# -*- coding:utf-8 -*-

from numpy import where
import pyautogui
import time

pyautogui.PAUSE = 1
pyautogui.FAILSAFE = True

file_head = 'baokemeng/'
file_ball = 'ball31.png'
file_array = ['CreatRoom.png', 'BeginGame.png', 'enter.png',  'InventoryPanel1.png', 'ball30.png',  # 删除'SelectLevel.png',
              'map.png']  # '2ball.png', 'map.png', 'SelectBB.png'  移除'SelBB.png'
file_exit_array = ['exit1.png', 'exit2.png', 'exit4.png', 'exit5.png']
file_begin_game = 'CreatGame.png'
#               , '2ball.png']
# 遇到的问题: 11对战平台应该是有防止自动脚本的,管理员身份运行
# 屏幕尺寸 1920 1080
# 创建房间鼠标大概位置 1121 181
# 下一步 实现发现没有点击到红球或者出现其他错误时 直接退出游戏重新开始，对应图片已截图   红球大概范围 1548 755 超出这个范围就视为出现bug可以重新开始游戏 或者难度选择错误选择了难度4或更高难度
# 完成游戏页面未截图 先完成一个球的test，然后再考虑两个球的问题
game_count = 0
while True:
    time.sleep(5)
    count_retry = 0
    similarity = 0.95
    selectBB = 0
    BB_x = 0
    BB_y = 0
    x_2 = 0  # 记录BB球地址
    y_2 = 0
    for i in range(0, 6):
        game_exit = 0
        if i > 3:
            for j in range(0, 4):
                file_name_exit = file_head + file_exit_array[j]
                # print("%s", file_name)
                coords4 = pyautogui.locateOnScreen(file_name_exit, confidence=1)
                if coords4 is not None:  # 每次都识别一次是否需要退出游戏
                    time.sleep(1)
                    print('退出——frond: ', j)
                    x1, y1 = pyautogui.center(coords4)
                    pyautogui.leftClick(x1, y1)
                    if j == 1:
                        game_exit = 1
                        game_count += 1
        if game_exit == 1:
            print('game_count:', game_count)
            break
        # print(pyautogui.position())
        file_name = file_head + file_array[i]
        move_count = 0
        if i == 0 or i == 1:
            coords = pyautogui.locateOnScreen(file_name, confidence=0.95)
        if i == 3:
            coords = pyautogui.locateOnScreen(file_name, confidence=0.90)
        if i == 4:  # 抓取红球地点
            file_name_ball = file_head + file_ball
            coords = pyautogui.locateOnScreen(file_name, confidence=0.97)
            coords2 = pyautogui.locateOnScreen(file_name_ball, confidence=0.97)
            if coords2 is not None and (coords is None):  # 抓取红球地点2
                print('替换')
                coords = coords2
        elif i == 6:
            coords = pyautogui.locateOnScreen(file_name, confidence=0.8)
        else:
            # print("%s", file_name)
            coords = pyautogui.locateOnScreen(file_name, confidence=0.9)
        # print("coords + %s", coords)
        while coords is None:
            # TODO 如果时间超过10s没有选择 那么就退出游戏重新开始
            # if i == 5 and move_count == 10:  # 如果找不到红球 就移动鼠标 采用小地图方式
            #     # file_name3 = file_head + file_array[6]
            #     # coords3 = pyautogui.locateOnScreen(file_name3, confidence=0.9)
            #     # x, y = pyautogui.center(coords3)
            #     # move_count += 1
            #     # pyautogui.moveTo(100, 1080)
            #     # # time.sleep(1)
            #     # pyautogui.moveTo(100, 1060)
            # print(pyautogui.position())
            count_retry += 1
            # print("coords + %s count_retry", count_retry, coords, file_name)
            print('retry ', count_retry, 'i:',  i)
            print('similarity', similarity)
            if i == 0 or i == 1:
                coords = pyautogui.locateOnScreen(file_name, confidence=0.95)
                if i == 0:
                    file_name_game = file_head + file_begin_game
                    coord_game = pyautogui.locateOnScreen(file_name_game, confidence=0.9)
                    print("coord_game")
                    if coord_game is not None:
                        print("coord_game2")
                        coords = coord_game
            elif i == 2:
                coords = pyautogui.locateOnScreen(file_name, confidence=0.7)
            # elif i == 4:
            #     # coords = pyautogui.locateOnScreen(file_name, confidence=similarity)
            #     # similarity = similarity - 0.05
            #     print('null')
            elif i == 4:  # 抓取红球地点
                similarity = similarity - 0.05
                file_name_ball = file_head + file_ball
                coords2 = pyautogui.locateOnScreen(file_name_ball, confidence=similarity) # , region=(600, 300, 2400, 900)
                coords = pyautogui.locateOnScreen(file_name, confidence=0.8)
                if coords2 is not None and (coords is None):  # 抓取红球地点2
                    print('替换')
                    coords = coords2
            elif i == 6:
                break
            else:
                if count_retry > 10:
                    coords = pyautogui.locateOnScreen(file_name, confidence=similarity)
                    print(i, ' else confidence-- retry')
                    similarity = similarity - 0.05
                else:
                    coords = pyautogui.locateOnScreen(file_name, confidence=0.9)
                    print(i, ' else confidence=0.9 retry')

            if similarity < 0.5:
                print("break")
                break

        if coords is not None:  # 图片识别的问题没有解决只能通过该方式实现鼠标模拟
            count_retry = 0
            similarity = 1
            print('find one : ', i)
            x, y = pyautogui.center(coords)
            if i == 3:  # 物品栏1，其实用快捷键更好
                print('物品栏点击')
                print(x, y)
                pyautogui.moveTo(944 + 350, 787 + 120)  # 使用物品
                pyautogui.leftClick(944 + 350, 787 + 120)  # 使用物品
                BB_x = 944 + 350
                BB_y = 787 + 120
                time.sleep(1)
                pyautogui.moveTo(194, 988)
                pyautogui.leftClick(194, 988)
                time.sleep(1)

            elif i == 2:  # 识别进入游戏
                print('进入游戏')
                print(x, y)
                time.sleep(1)
                pyautogui.moveTo(960,  380)
                time.sleep(2)
                pyautogui.leftClick(960,  420)
                time.sleep(1)

            # elif i == 3:  # 选择难度
            #     print('选择游戏难度')
            #     # print(x, y)
            #     # pyautogui.leftClick(x, y)
            #     pyautogui.leftClick(958, 332 + 20)
            #     # time.sleep(1)
            #     print(x, y)
            #     # pyautogui.scroll(-500, x=None, y=None)
            #     # pyautogui.scroll(-500, x=None, y=None)
            #     # pyautogui.scroll(-500, x=None, y=None)
            #     time.sleep(1)

            elif i == 4:  # 识别红球
                print('识别红球')
                print(x, y)
                time.sleep(1)
                pyautogui.rightClick(x, y)
                pyautogui.rightClick(x, y)
                pyautogui.rightClick(x, y)
                x_2 = x
                y_2 = y
                time.sleep(1)
                # time.sleep(1)

            elif i == 5:
                print('移动map')
                print(x, y)
                pyautogui.moveTo(190 + 80, 945 - 108)
                pyautogui.leftClick(190 + 80, 945 - 108)
                time.sleep(1)  # 延迟1S确定鼠标已经点完了
                print('选择BB')
                selectBB = 1
                pyautogui.moveTo(BB_x, BB_y)
                pyautogui.leftClick(BB_x, BB_y)
                time.sleep(1)
                pyautogui.moveTo(500+450,  500+115)
                # time.sleep(1)
                pyautogui.leftClick(500+450,  500+115)
                # time.sleep(1)
                pyautogui.moveTo(194, 988)
                pyautogui.leftClick(194, 988)
                time.sleep(1)
                # pyautogui.leftClick(194, 988)
                pyautogui.rightClick(x_2, y_2)
                pyautogui.rightClick(x_2, y_2)
                pyautogui.rightClick(x_2, y_2)
                time.sleep(1)
                # pyautogui.rightClick(x_2, y_2)
                # pyautogui.leftClick(190 + 80, 945 - 108)
                pyautogui.moveTo(190 + 80, 945 - 108)
                pyautogui.leftClick(190 + 80, 945 - 108)
                time.sleep(1)
                pyautogui.moveTo(BB_x, BB_y)
                pyautogui.leftClick(BB_x, BB_y)
                time.sleep(1)
                pyautogui.moveTo(500 - 80, 500 + 120)
                # time.sleep(1)
                pyautogui.leftClick(500 - 80, 500 + 120)
                # pyautogui.leftClick(194, 988)
                # pyautogui.leftClick(BB_x - 820, BB_y - 280)
                time.sleep(2)
            else:
                if i == 3:
                    print('i == 3  continue')
                    continue
                print(x, y)
                # pyautogui.moveTo(x, y)
                pyautogui.leftClick(x, y)
    while True:
        game_exit = 0
        for j in range(0, 4):
            file_name_exit = file_head + file_exit_array[j]
            print("正在识别退出 ", j)
            coords4 = pyautogui.locateOnScreen(file_name_exit, confidence=0.9)

            if coords4 is not None:  # 每次都识别一次是否需要退出游戏
                time.sleep(1)
                print('退出', j)
                x1, y1 = pyautogui.center(coords4)
                pyautogui.moveTo(x1, y1)
                pyautogui.leftClick(x1, y1)
                print('game_count:', game_count)
                if j == 1:
                    game_exit = 1
                    game_count += 1
        if game_exit == 1:
            break
    time.sleep(20)
            # else:
            #     # pyautogui.leftClick(1121, 181)
            # print('count i : ', i)
